<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /**
       * @param {string} s
       * @return {boolean}
       */
      var isValid = function (s) {
        let stack = [];
        for (let i = 0; i < s.length; i++) {
          const c = s[i];
          if (c == "{" || c == "(" || c == "[") {
            stack.push(c);
          } else {
            if (stack.length == 0) {
              return false;
            }
            const top = stack[stack.length - 1];
            if (
              (top == "(" && c == ")") ||
              (top == "{" && c == "}") ||
              (top == "[" && c == "]")
            ) {
              stack.pop();
            } else {
              return false;
            }
          }
        }
        return stack.length == 0;
      };
    </script>
  </body>
</html>
